

#include <material/shadereditor/shadertreemodel.h>
#include <qmimedata.h>


cceMaterialShaderTreeModel::cceMaterialShaderTreeModel (cceMaterialManager* mgr)
	: QAbstractItemModel ()
	, _mgr (mgr)
{
  connect (_mgr, SIGNAL(ShaderAdded (cceMaterialShader*)), this, SLOT(ResetContent ()));
  connect (_mgr, SIGNAL(ShaderChanged (cceMaterialShader*)), this, SLOT(ResetContent ()));
  connect (_mgr, SIGNAL(ShaderRemoved (cceMaterialShader*)), this, SLOT(ResetContent ()));

  setSupportedDragActions (Qt::ActionMask);
}

void cceMaterialShaderTreeModel::ResetContent()
{
  reset ();
}

int cceMaterialShaderTreeModel::columnCount (const QModelIndex&) const
{
	return Column_COUNT;
}

QVariant cceMaterialShaderTreeModel::data (const QModelIndex& index, int role) const
{
	if (!index.isValid ())
		{
			return QVariant ();
		}

	cceMaterialShader* shader = static_cast<cceMaterialShader*>(index.internalPointer ());

	if (role == Qt::DisplayRole)
		{
			switch (index.column ())
				{
				case Column_Name:
					return QVariant (shader->GetName ());
				case Column_Type:
          return cceMaterialShader::GetTypeName(shader->GetType());
				default:
					break;
				}
		}

	return QVariant ();
}

QModelIndex cceMaterialShaderTreeModel::index (int row, int column, const QModelIndex &parent) const
{
	if (parent.isValid ()) return QModelIndex ();
	if (column < 0 || column >= Column_COUNT) return QModelIndex ();
	if (row < 0 || row >= _mgr->GetNumberOfShaders ()) return QModelIndex ();

	return createIndex (row, column, _mgr->GetShader (row));
}

QModelIndex cceMaterialShaderTreeModel::parent (const QModelIndex &index) const
{
	return QModelIndex ();
}

int cceMaterialShaderTreeModel::rowCount (const QModelIndex &parent) const
{
	if (parent.isValid ()) return 0;
	return _mgr->GetNumberOfShaders ();
}

Qt::ItemFlags cceMaterialShaderTreeModel::flags (const QModelIndex& index) const
{
	Qt::ItemFlags f = QAbstractItemModel::flags (index);
	if (index.isValid()) 
		{
			f |= Qt::ItemIsDragEnabled;
		}
	return f;
}

QString cceMaterialShaderTreeModel::GetShaderName (unsigned row) const
{
  if (row < 0 || row >= _mgr->GetNumberOfShaders()) return QString::Null ();
  cceMaterialShader* shader = _mgr->GetShader (row);
  if (!shader) return QString::Null ();
  return shader->GetName ();
}

QString cceMaterialShaderTreeModel::GetShaderTypeName (unsigned row) const
{
  if (row < 0 || row >= _mgr->GetNumberOfShaders()) return QString::Null ();
  cceMaterialShader* shader = _mgr->GetShader (row);
  if (!shader) return QString::Null ();

  return cceMaterialShader::GetTypeName(shader->GetType());
}


QMimeData* cceMaterialShaderTreeModel::mimeData (const QModelIndexList& indices) const
{
	if (indices.size () == 0)
		{
			return 0;
		}
	const QModelIndex& index = indices[0];
	if (!index.isValid ())
		{
		}

	int row = index.row ();
	foreach (const QModelIndex& idx, indices)
		{
			if (!idx.isValid () || idx.row () != row)
				{
					return 0;
				}
		}

	cceMaterialShader* shader = static_cast<cceMaterialShader*>(index.internalPointer ());
	if (!shader)
		{
			return 0;
		}
	
	QMimeData* mimeData = new QMimeData ();
	mimeData->setData (CCE_MATERIAL_SHADERNAME_MIME, shader->GetName ().toAscii ());
	return mimeData;
}

		
QVariant cceMaterialShaderTreeModel::headerData (int section, Qt::Orientation orientation, int role) const
{
	if (section < 0 || section >= Column_COUNT || role != Qt::DisplayRole)
		{
			return QVariant ();
		}
	Q_UNUSED (orientation);
	switch (section)
		{
		case Column_Name:
			return "Name";
		case Column_Type:
			return "Type";
		default:
			break;
		}
	return QVariant ();
}

